package com.anlu.alspringmvc.spring.framework.webmvc.servlet;

import java.io.File;

public class ALViewResolver {

    private final  String DEFAULT_TEMPLATE_SUFFIX = ".html";

    private File templateRootDir;

    public ALViewResolver(String templateRoot) {
        String templateRootPath = this.getClass().getClassLoader().getResource(templateRoot).getFile();
        templateRootDir = new File(templateRootPath);
    }

    /**
     * 解析视图名称
     * @param viewName 名称
     * @return
     */
    public ALView resolveViewName(String viewName){
        if(null == viewName || "".equals(viewName.trim())){
            return null;
        }
        viewName = viewName.endsWith(DEFAULT_TEMPLATE_SUFFIX) ? viewName : (viewName + DEFAULT_TEMPLATE_SUFFIX);
        File templateFile = new File((templateRootDir.getPath() + "/" + viewName).replaceAll("/+","/"));
        return new ALView(templateFile);
    }
}
